//package io.renren.modules.app.message.handler;
//
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import io.renren.modules.app.message.MessageResult;
//import io.renren.modules.app.message.MsgType;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//import java.util.List;
//import java.util.Objects;
//import java.util.concurrent.LinkedBlockingDeque;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author :
// * @version V1.0
// * @Project:
// * @Package
// * @Description: 订单(商品)消息处理
// * @date
// **/
//@Component(OrderMessageHandler.BeanName)
//@Slf4j
//public class OrderMessageHandler extends AbstractJdVopMessageHandler {
//
//    public static final String BeanName="MessageOrderHandle";
//    /**
//     * 订单取消处理消息条数
//     */
//    private static Integer CANCEL=0;
//    /**
//     * 配送消息条数
//     */
//    private static Integer DISPATCH=0;
//
//    /**
//     * 订单完成消息条数
//     */
//    private static Integer COMPLETE=0;
//
//    /**
//     * 新订单消息条数
//     */
//    private static Integer CREATED=0;
//    /**
//     * 待收货订单消息条数
//     */
//    private static Integer WAIT_RECEIVING=0;
//    /**
//     * 订单消息总条数条数
//     */
//    private static Integer COUNT=0;
//
//
////    @Autowired
////    private IOmsOrderService omsOrderService;
//
////    @Autowired
////    private IOmsOrderReturnApplyService omsOrderReturnApplyService;
//
////    @Autowired
////    private IOmsOrderItemService omsOrderItemService;
//
//
//    private ThreadPoolExecutor orderExecutor= new ThreadPoolExecutor(1,5, 10,TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>(200));
//
//    public OrderMessageHandler(ApplicationContext applicationContext) {
//        super(applicationContext);
//        setBeanName(BeanName);
//        orderExecutor.allowCoreThreadTimeOut(true);
//    }
//    @Override
//    public void read(MessageResult msg) {
//        ++COUNT;
//        log.info("订单消息处理条数：{}，时间：{}",COUNT,new Date());
////        int type=msg.getType();
////        switch (type){
////            case MsgType.Order.CANCEL:
////                orderExecutor.execute(()-> this.handleOrderCancel(msg));
////            case MsgType.Order.DISPATCH:
////                orderExecutor.execute(()-> this.handleOrderDispatch(msg));
////               break;
////            case MsgType.Order.COMPLETE:
////                orderExecutor.execute(()-> this.handleOrderComplete(msg));
////               break;
////            case MsgType.Order.CREATED:
////                orderExecutor.execute(()-> this.handleOrderCreate(msg));
////                break;
////            case MsgType.Order.AFTER_SALE:
////                orderExecutor.execute(()-> this.handleOrderReturnApply(msg));
////                break;
////            case MsgType.Order.WAIT_RECEIVING:
////             default:
////                log.info("JD_VOP NOT FOUND FUN：{}",msg);
////                notifyMsgReadEvent(msg,false);
////        }
//    }
//
//
//    @Override
//    public void batchRead(List<MessageResult> msgs) {
//        System.out.println("订单处理");
//        msgs.parallelStream().forEach(this::read);
//    }
//
//    /**
//     * 处理创建订单消息
//     * @param msg
//     */
//    private void handleOrderCreate(MessageResult msg){
//        ++CREATED;
//        log.info("订单新建消息处理条数：{}，时间：{}",CREATED,new Date());
//        log.info("jd_order_creat:{}",msg);
//        notifyMsgReadEvent(msg,true);
//    }
//
//    /**
//     * 取消订单
//     * 订单状态：待付款更改为已取消状态
//     * @param msg
//     */
//    public void handleOrderCancel(MessageResult msg){
//        ++CANCEL;
//        log.info("订单取消消息处理条数：{}，时间：{}",CANCEL,new Date());
//        log.info("jd_order_cancel:{}",msg);
////        OmsOrder omsOrder = getOmsOrder(msg);
////        boolean flag=true;
////        if(Objects.nonNull(omsOrder) && Integer.valueOf(OrderStatus.INIT.getValue()).equals(omsOrder.getStatus())) {
////            flag = this.updateOrderStatus(omsOrder.getOutOrderSn(), OrderStatus.CLOSED.getValue());
////        }
////        notifyMsgReadEvent(msg,flag);
//    }
//    /**
//     * 更新订单状态为配送状态并推送删除消息
//     * 待发货状态更改为已发货(待收货)状态
//     * @param msg
//     */
//    public void handleOrderDispatch(MessageResult msg){
//        ++DISPATCH;
//        log.info("订单发货消息处理条数：{}，时间：{}",DISPATCH,new Date());
//        log.info("jd_order_dispatch:{}",msg);
////        OmsOrder omsOrder = getOmsOrder(msg);
////        boolean flag=true;
////        if(Objects.nonNull(omsOrder) && Integer.valueOf(OrderStatus.TO_DELIVER.getValue()).equals(omsOrder.getStatus())) {
////            flag = this.updateOrderStatus(omsOrder.getOutOrderSn(), OrderStatus.DELIVERED.getValue());
////        }
////        notifyMsgReadEvent(msg,flag);
//    }
//
////    public OmsOrder getOmsOrder(MessageResult msg) {
////        String jdOrderId = getMsg(msg, MessageResult.orderId);
////        return getOneByOutOrderSn(jdOrderId);
////    }
//
//    /**
//     * 更新订单状态为完成状态
//     * 待收货状态更改为已完成状态
//     * @param msg
//     */
//    private void handleOrderComplete(MessageResult msg){
//        ++COMPLETE;
//        log.info("订单完成消息处理条数：{}，时间：{}",COMPLETE,new Date());
//        log.info("jd_order_complete:{}",msg);
////        String jdOrderId=getMsg(msg,MessageResult.orderId);
////        OmsOrder omsOrder= getOneByOutOrderSn(jdOrderId);
////        boolean flag=true;
////        if(Objects.nonNull(omsOrder) && Integer.valueOf(OrderStatus.DELIVERED.getValue()).equals(omsOrder.getStatus())){
////            flag=this.updateOrderStatus(jdOrderId,OrderStatus.TRADE_SUCCESS.getValue());
////        }
////        notifyMsgReadEvent(msg,flag);
//    }
//
//
//    /**
//     * 更新订单状态
//     * @param outOrderSn
//     * @param status
//     */
//    public boolean updateOrderStatus(String outOrderSn,Integer status){
////        if(StrUtil.isBlank(outOrderSn) || status == null || status == 0) return true;
////        return this.omsOrderService.update(Wrappers.<OmsOrder>lambdaUpdate().set(OmsOrder::getStatus,status).eq(OmsOrder::getOutOrderSn,outOrderSn));
//        return true;
//    }
//
//
//
//
//    /**
//     * 根据京东订单号获取订单
//     * @param outOrderSn
//     * @return
//     */
////    private OmsOrder getOneByOutOrderSn(String outOrderSn){
////        return this.omsOrderService.getOne(Wrappers.<OmsOrder>lambdaQuery().eq(OmsOrder::getOutOrderSn,outOrderSn));
////    }
//
//    /**
//     * 处理创建订单售后服务单变更
//     * @param msg
//     */
//    private void handleOrderReturnApply(MessageResult msg){
//        log.info("jd_order_return_apply:{}",msg);
////        OmsOrder omsOrder = getOmsOrder(msg);
////        String state = getMsg(msg, MessageResult.goodsState);
////        String afsServiceId = getMsg(msg, MessageResult.afsServiceId);
////        String skuId = getMsg(msg, MessageResult.skuId);
////        boolean flag=true;
////        OmsOrderReturnApply oldApply = omsOrderReturnApplyService.getOne(new QueryWrapper<OmsOrderReturnApply>()
////                .eq("order_id",omsOrder.getId())
////                .eq("product_id",skuId)
////        );
////        if(!ObjectUtil.isNull(omsOrder) && !ObjectUtil.isNull(oldApply)){
////            int status =Integer.parseInt(state);
////            switch (status){
////                case 1:   //创建服务单
////                case 4:   //服务单审核通过
////                case 5:   //服务单用户确认
////                    flag = omsOrderReturnApplyService.update(new UpdateWrapper<OmsOrderReturnApply>()
////                            .eq("id",oldApply.getId())
////                            .set("service_id",afsServiceId)
////                            .set("service_status",status)
////                    );
////                    break;
////                case 2:   //服务单审核不通过
////                    flag = omsOrderReturnApplyService.update(new UpdateWrapper<OmsOrderReturnApply>()
////                            .eq("id",oldApply.getId())
////                            .set("service_id",afsServiceId)
////                            .set("service_status",status)
////                            .set("deal_status",5)
////                            .set("deal_status","服务单审核不通过")
////                    );
////                    if(flag){
////                        omsOrderItemService.update(new UpdateWrapper<OmsOrderItem>()
////                                .eq("order_id",oldApply.getOrderId())
////                                .eq("product_id",oldApply.getProductId())
////                                .set("return_status",5)
////                        );
////                    }
////                    break;
////                case 3:   //服务单审核取消
////                    flag = omsOrderReturnApplyService.update(new UpdateWrapper<OmsOrderReturnApply>()
////                            .eq("id",oldApply.getId())
////                            .set("service_id",afsServiceId)
////                            .set("service_status",status)
////                            .set("deal_status",5)
////                            .set("deal_status","服务单取消")
////                    );
////                    if(flag){
////                        omsOrderItemService.update(new UpdateWrapper<OmsOrderItem>()
////                                .eq("order_id",oldApply.getOrderId())
////                                .eq("product_id",oldApply.getProductId())
////                                .set("return_status",5)
////                        );
////                    }
////                    break;
////                case 6:   //服务单完成
////                    flag = omsOrderReturnApplyService.update(new UpdateWrapper<OmsOrderReturnApply>()
////                            .eq("id",oldApply.getId())
////                            .set("deal_status",4)
////                            .set("service_id",afsServiceId)
////                            .set("service_status",status)
////                    );
////                    if(flag){
////                        omsOrderItemService.update(new UpdateWrapper<OmsOrderItem>()
////                                .eq("order_id",oldApply.getOrderId())
////                                .eq("product_id",oldApply.getProductId())
////                                .set("return_status",4)
////                        );
////                    }
////                    break;
////                default:
////                    log.info("JD_VOP NOT FOUND FUN：{}",msg);
////            }
////            notifyMsgReadEvent(msg,flag);
////        }
//    }
//}
